package chapter12_14;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class StandardExamInTime extends JFrame implements ActionListener,ItemListener{
   File f_testFile;
   int MAX = 8;
   int maxTime = MAX,score=0;
   javax.swing.Timer time;  //计时器
   JTextArea textArea_showQuesion; //显示试题
   JCheckBox checkBox_choiceA, checkBox_choiceB, checkBox_choiceC, checkBox_choiceD;
   JLabel lable_ShowScore, label_ShowTime;
   String sCorrectAnswer;   //正确答案
   JButton button_reStart;
   FileReader fReader_inOne;
   BufferedReader bufferReader_inTwo;
   StandardExamInTime(){
      time = new javax.swing.Timer(1000,this);
      textArea_showQuesion = new JTextArea(2,16);
      setLayout(new FlowLayout());
      lable_ShowScore =new JLabel("分数:"+score);
      label_ShowTime =new JLabel("时间： ");
      add(label_ShowTime);
      add(new JLabel("问题:")) ;
      add(textArea_showQuesion);
      checkBox_choiceA =new JCheckBox("A");
      checkBox_choiceB =new JCheckBox("B");
      checkBox_choiceC =new JCheckBox("C");
      checkBox_choiceD =new JCheckBox("D");
      checkBox_choiceA.addItemListener(this);
      checkBox_choiceB.addItemListener(this);
      checkBox_choiceC.addItemListener(this);
      checkBox_choiceD.addItemListener(this);
      add(checkBox_choiceA);
      add(checkBox_choiceB);
      add(checkBox_choiceC);
      add(checkBox_choiceD);
      add(lable_ShowScore);
      button_reStart =new JButton("再做一遍");
      button_reStart.setEnabled(false);
      add(button_reStart);
      button_reStart.addActionListener(this);
      setBounds(100,100,200,200);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
   }
   public void setMAX(int n){
      MAX = n;
   }
   public void setF_testFile(File f) {
      f_testFile = f;
      score=0;
      try{
         fReader_inOne = new FileReader(f_testFile);
         bufferReader_inTwo = new BufferedReader(fReader_inOne);
         readOneQuesion();
         button_reStart.setEnabled(false);
      }
      catch(IOException exp){
         textArea_showQuesion.setText("没有选题");
         exp.printStackTrace();
      }
   }
   public void readOneQuesion() {
      textArea_showQuesion.setText(null);
      try {
         String s = null;
         while((s = bufferReader_inTwo.readLine())!=null) {
            if(!s.startsWith("-"))
               textArea_showQuesion.append("\n"+s);
            else {
               s = s.replaceAll("-","");
               sCorrectAnswer = s;
               System.out.println("sCorrectAnswer="+sCorrectAnswer);
               break;
            }
         }
         time.start();   //启动计时
         if(s==null) {
            bufferReader_inTwo.close();
            button_reStart.setEnabled(true);
            textArea_showQuesion.setText("题目完毕");
            time.stop();
         }
      }
      catch(IOException exp){
         exp.printStackTrace();
      }
   }
   public void itemStateChanged(ItemEvent e) {
      JCheckBox checkbox=(JCheckBox)e.getSource();
      String str=checkbox.getText();
      boolean booOne=checkbox.isSelected();
      boolean booTwo=str.compareToIgnoreCase(sCorrectAnswer)==0;
      if(booOne&&booTwo){
         score++;
         lable_ShowScore.setText("分数:"+score);
         time.stop();       //停止计时
         maxTime = MAX;
         readOneQuesion(); //读入下一道题目
      }
      checkbox.setSelected(false);
   }
   public void actionPerformed(ActionEvent e) {
      if(e.getSource()==time){
         label_ShowTime.setText("剩:"+maxTime+"秒");
         maxTime--;
         if(maxTime <= 0){
            maxTime = MAX;
            readOneQuesion(); //读入下一道题目
         }
      }
      else if(e.getSource()== button_reStart) {
         setF_testFile(f_testFile);
      }
   }
}
